<template>
  <div :class="{'show':show}" >
    <svg-icon class-name="search-icon" icon-class="nested" @click.stop="click" />
   
    <el-dialog title="系统设置" :visible.sync="show" 
    :close-on-click-modal="tab.closeOnClickModal"  
    :modal="tab.modal"
    :modal-append-to-body="tab.modalAppendToBody"
    >
      <el-tabs v-model="activeName" type="border-card" @tab-click="handleClick">
        <el-tab-pane label="全局设置" name="global">


          <el-radio-group v-model="organQueryModel" @change="organQueryModelChange">
            <el-radio v-model="organQueryModel" label="selfAndSub" border>查本级组织及以下</el-radio>
            <el-radio v-model="organQueryModel" label="selected" border >仅查选中组织</el-radio>
          </el-radio-group>


        </el-tab-pane>
        <el-tab-pane label="租户设置" name="tenant">
       
          <el-radio-group v-model="tenantQueryModel" @change="organQueryModelChange">
            <el-radio v-model="tenantQueryModel" label="insulate" border>租户隔离</el-radio>
            <el-radio v-model="tenantQueryModel" label="temporary" border >平台临时模式</el-radio>
          </el-radio-group>
        </el-tab-pane>

        <el-tab-pane label="临时设置" name="current">
          <el-button type="primary">获取系统参数</el-button>
          <el-button type="primary">获取租户参数</el-button>
          <el-button type="primary">获取用户参数</el-button>
        </el-tab-pane>
     

        <el-tab-pane label="我的待办" name="todoList">
          <el-button type="primary">获取系统参数</el-button>
          <el-button type="primary">获取租户参数</el-button>
          <el-button type="primary">获取用户参数</el-button>
        </el-tab-pane>
    

        <el-tab-pane label="我的导出" name="myexp">
          <el-button type="primary">获取系统参数</el-button>
          <el-button type="primary">获取租户参数</el-button>
          <el-button type="primary">获取用户参数</el-button>
        </el-tab-pane>
      </el-tabs>


      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogFormVisible = false">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import Fuse from 'fuse.js'
import path from 'path'
import i18n from '@/lang'
import localStorageUtil from '@/utils/localStorageUtil'


export default {
  name: 'SystemSetting',
  data() {
    return {
      tab: {
        closeOnClickModal: false,
        modal: true,
        modalAppendToBody: false
      },
      activeName:'global',
      organQueryModel:'',
      tenantQueryModel:'',
      search: '',
      options: [],
      searchPool: [],
      show: false,
      fuse: undefined
    }
  },
  computed: {
    routes() {
      return this.$store.getters.permission_routes
    },
    lang() {
      return this.$store.getters.language
    }
  },
  watch: {

    lang() {
      this.searchPool = this.generateRoutes(this.routes)
    },
    routes() {
      this.searchPool = this.generateRoutes(this.routes)
    },
    searchPool(list) {
      this.initFuse(list)
    },
    show(value) {
      if (value) {
        document.body.addEventListener('click', this.close)
      } else {
        document.body.removeEventListener('click', this.close)
      }
    }
  },
  mounted() {
    this.searchPool = this.generateRoutes(this.routes)
  },
  methods: {
    click() {
      this.show = !this.show
      if (this.show) {
        this.$refs.headerSearchSelect && this.$refs.headerSearchSelect.focus()
      }
    },
    close() {
      this.$refs.headerSearchSelect && this.$refs.headerSearchSelect.blur()
      this.options = []
      this.show = false
    },
    change(val) {
      this.$router.push(val.path)
      this.search = ''
      this.options = []
      this.$nextTick(() => {
        this.show = false
      })
    },
    initFuse(list) {
      this.fuse = new Fuse(list, {
        shouldSort: true,
        threshold: 0.4,
        location: 0,
        distance: 100,
        maxPatternLength: 32,
        minMatchCharLength: 1,
        keys: [{
          name: 'title',
          weight: 0.7
        }, {
          name: 'path',
          weight: 0.3
        }]
      })
    },
    // Filter out the routes that can be displayed in the sidebar
    // And generate the internationalized title
    generateRoutes(routes, basePath = '/', prefixTitle = []) {
      let res = []

      for (const router of routes) {
        // skip hidden router
        if (router.hidden) { continue }

        const data = {
          path: path.resolve(basePath, router.path),
          title: [...prefixTitle]
        }

        if (router.meta && router.meta.title) {
          // generate internationalized title
          const i18ntitle = i18n.t(`route.${router.meta.title}`)

          data.title = [...data.title, i18ntitle]

          if (router.redirect !== 'noredirect') {
            // only push the routes with title
            // special case: need to exclude parent router without redirect
            res.push(data)
          }
        }

        // recursive child routes
        if (router.children) {
          const tempRoutes = this.generateRoutes(router.children, data.path, data.title)
          if (tempRoutes.length >= 1) {
            res = [...res, ...tempRoutes]
          }
        }
      }
      return res
    },
    querySearch(query) {
      if (query !== '') {
        this.options = this.fuse.search(query)
      } else {
        this.options = []
      }
    },
    organQueryModelChange(label){
      localStorageUtil.save('SYSTEM_SETTING_ORGAN_QUERY_MODEL', label)
    },
    handleClick(){

    }
  }
}
</script>

<style lang="scss" scoped>
.header-search {
  font-size: 0 !important;

  .search-icon {
    cursor: pointer;
    font-size: 18px;
    vertical-align: middle;
  }

  .header-search-select {
    font-size: 18px;
    transition: width 0.2s;
    width: 0;
    overflow: hidden;
    background: transparent;
    border-radius: 0;
    display: inline-block;
    vertical-align: middle;

    /deep/ .el-input__inner {
      border-radius: 0;
      border: 0;
      padding-left: 0;
      padding-right: 0;
      box-shadow: none !important;
      border-bottom: 1px solid #d9d9d9;
      vertical-align: middle;
    }
  }

  &.show {
    .header-search-select {
      width: 210px;
      margin-left: 10px;
    }
  }
}
</style>
